package com.stylefeng.guns.modular.manage.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.system.model.Car;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.CarMa;
import com.stylefeng.guns.modular.manage.service.ICarMaService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 车辆管理控制器
 *
 * @author fengshuonan
 * @Date 2018-08-31 15:43:09
 */
@Controller
@RequestMapping("/carMa")
public class CarMaController extends BaseController {

    private String PREFIX = "/manage/carMa/";

    @Autowired
    private ICarMaService carMaService;

    /**
     * 跳转到车辆状态首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "carMa.html";
    }

    /**
     * 跳转到添加车辆状态
     */
    @RequestMapping("/carMa_add")
    public String carMaAdd() {
        return PREFIX + "carMa_add.html";
    }

    /**
     * 跳转到修改车辆状态
     */
    @RequestMapping("/carMa_update/{carMaId}")
    public String carMaUpdate(@PathVariable Integer carMaId, Model model) {
        CarMa carMa = carMaService.selectById(carMaId);
        model.addAttribute("item",carMa);
        LogObjectHolder.me().set(carMa);
        return PREFIX + "carMa_edit.html";
    }

    /**
     * 获取车辆状态列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
//    public Object list(String condition) {
//        return carMaService.selectList(null);
//    }

    public Object list(String plateNumbr,Integer status) {
        List<CarMa> listMa = carMaService.getCarMa(plateNumbr,status);
        System.out.println("aaaaaa");
        return listMa;
    }


    /**
     * 删除车辆状态
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer carMaId) {
        carMaService.deleteById(carMaId);
        return SUCCESS_TIP;
    }

    /**
     * 修改车辆状态
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CarMa carMa) {
        carMaService.updateById(carMa);
        return SUCCESS_TIP;
    }

    /**
     * 车辆状态详情
     */
    @RequestMapping(value = "/detail/{carMaId}")
    @ResponseBody
    public Object detail(@PathVariable("carMaId") Integer carMaId) {
        return carMaService.selectById(carMaId);
    }






    @RequestMapping(value = "/download")
    @ResponseBody
    public void download(HttpServletResponse response ,CarMa ma) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("车辆状态表");

        List<CarMa>  listMa = carMaService.selectList(null);

        String fileName = "车辆状态"  + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据

        int rowNum = 1;

        String[] headers = { "编号", "车牌号", "状态", "时间"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (CarMa carMa : listMa) {
//            DateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String ortime=format.format(carMa.getOrTime());
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(carMa.getId());
            row1.createCell(1).setCellValue(carMa.getPlateNumbr());
            row1.createCell(2).setCellValue(carMa.getStatusView());
            row1.createCell(3).setCellValue(carMa.getOrTime());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        System.out.println("测试导出excel");
        workbook.write(response.getOutputStream());
    }



}
